using System;
using NMollom.Internal;

namespace NMollom {
    /// <summary>
    /// Base class for a captcha.
    /// </summary>
    [Serializable]
    public abstract class Captcha {
        private readonly string url;
        private readonly string sessionId;
        [NonSerialized]
        private readonly Mollom mollom;

        protected Captcha(CaptchaResult result) {
            url = result.url;
            sessionId = result.sessionId;
            this.mollom = null;
        }

        protected Captcha(CaptchaResult result, Mollom mollom) {
            url = result.url;
            sessionId = result.sessionId;
            this.mollom = mollom;
        }

        public string Url {
            get { return url; }
        }

        public string SessionId {
            get { return sessionId; }
        }

        public bool Check(string solution) {
            if (mollom == null)
                throw new InvalidOperationException("No mollom instance attached.");
            return Check(mollom, solution);
        }

        public bool Check(Mollom useMollom, string solution) {
            if (useMollom == null)
                throw new ArgumentNullException("useMollom");
            return useMollom.CheckCaptcha(this, solution);
        }
    }

    [Serializable]
    public class ImageCaptcha : Captcha {
        public ImageCaptcha(CaptchaResult result)
            : base(result) {
        }

        public ImageCaptcha(CaptchaResult result, Mollom mollom)
            : base(result, mollom) {
        }
    }

    [Serializable]
    public class AudioCaptcha : Captcha {
        public AudioCaptcha(CaptchaResult result)
            : base(result) {
        }

        public AudioCaptcha(CaptchaResult result, Mollom mollom)
            : base(result, mollom) {
        }
    }
}
